<template>
  <div>
    <el-table
      :data="treeDate"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="id标志" sortable width="180">
      </el-table-column>
      <el-table-column prop="pid" label="父级标志" sortable width="180">
      </el-table-column>
      <el-table-column prop="name" label="地址"> </el-table-column>
      <el-table-column>
        <template>
          <el-button icon="el-icon-edit">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
var arr = [
  { id: 1, pid: 0, name: "中国" },
  { id: 2, pid: 1, name: "湖北省" },
  { id: 3, pid: 2, name: "武汉市" },
  { id: 4, pid: 2, name: "恩施州" },
  { id: 5, pid: 1, name: "四川省" },
  { id: 6, pid: 0, name: "中国1" },
  { id: 7, pid: 0, name: "中国12" },
  { id: 8, pid: 0, name: "中国134" },
];
function tree(object, parent) {
  let treeList = [];

  object.map((res, index) => {
    if (res.pid === parent) {
      // 第一次循环表示有多少符合条件的一级
      let obj = res; // 拿到符合条件 对象
      let temp;
      temp = tree(object, obj.id); // 拿到父级的id 递归 对比pid
      // 比如 拿到id为1 进入递归 对比 res.pid === 1 拿到结果
      if (temp.length > 0) {
        // 查询有结果 就 赋值
        obj.children = temp;
      }
      treeList.push(obj);
    }
  });
  return treeList;
}
var arr1 = tree(arr, 0);
// console.log() // 数组 0为默认的父id
export default {
  data() {
    return {
      treeDate: arr1,
      tableData: [
        {
          id: 1,
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          id: 2,
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          id: 3,
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
          children: [
            {
              id: 31,
              date: "2016-05-01",
              name: "王小虎",
              address: "上海市普陀区金沙江路 1519 弄",
            },
            {
              id: 32,
              date: "2016-05-01",
              name: "王小虎",
              address: "上海市普陀区金沙江路 1519 弄",
            },
          ],
        },
        {
          id: 4,
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
      tableData1: [
        {
          id: 1,
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          id: 2,
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          id: 3,
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
          hasChildren: true,
        },
        {
          id: 4,
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
    };
  },
  methods: {
    load(tree, treeNode, resolve) {
      setTimeout(() => {
        resolve([
          {
            id: 31,
            date: "2016-05-01",
            name: "王小虎",
            address: "上海市普陀区金沙江路 1519 弄",
          },
          {
            id: 32,
            date: "2016-05-01",
            name: "王小虎",
            address: "上海市普陀区金沙江路 1519 弄",
          },
        ]);
      }, 1000);
    },
  },
};
</script>
